import { Component } from '@angular/core';
import { Storage } from '@ionic/storage';
import { NavController,NavParams,ToastController } from 'ionic-angular';
import { EditorService } from '../../providers/EditorService';

import { ApiService } from '../../providers/ApiService';

@Component({
  selector: 'page-taskdetail',
  templateUrl: 'taskdetail.html'
})
export class TaskDetail {
  state;
  rate;
  id;
  token;
  qualityrate;
  active = false;
  tasklog = []
  content = {
    createuser:'',
    receiveuser:''
  }
  dateMin;
  dateMax;
  incomplete_vaule;
  form_content = {
    instruction:'',
    recipient:'',
    end_time:''
  }
  body = {};
  operation_state = '';
  efficiency = [{ id:1 }, { id:2 }, { id:3 }, { id:4 }, { id:5 }]
  allcheckman = []
  username = '';
  expectedtime;
  efficiency_score;
  quality_score;
  check_status = false;
  submit_status = false;
  receive_status = false;
  showsubmit = true;
  showsubmit1 = true;
  remind = false;
  constructor(
    private navParams: NavParams,
    public navCtrl: NavController,
    public storage: Storage,
    private apiService: ApiService,
    public toastCtrl: ToastController,
    private EditorService: EditorService,
  ) {
    //this.state = navParams.get('state');
    this.id = navParams.get('id');
    this.dateMin = this.formartDate(new Date());
    this.dateMax = this.formartDate(new Date(2100, 1, 1));
    this.form_content.end_time = this.dateMin;

    this.storage.get('mytoken').then((mytoken)=>{
      if(mytoken) {
        this.token = mytoken;
      }
      this.getTaskDetail();
      this.getUser();
    })
    this.storage.get('username').then((username)=>{
      if(username) {
        this.username = username;
      }
    })
  }
  ionViewDidEnter(){
    if(this.content.createuser != this.username && this.state == 'check_state'){
      this.showsubmit = false;
    }
    if(this.state == 'receive_state' && this.content.receiveuser != this.username){
      this.showsubmit1 = false;
    }
    if(this.state == 'receive_state' && this.content.createuser == this.username){
      this.remind = true;
    }
  }
  private dismissHandler() {
    //console.info('Toast onDidDismiss()');
  }
  //消息提示函数
  formTip(hint){
    const toast = this.toastCtrl.create({
      message: hint,
      duration: 3000,
      position: 'middle'
    });
    toast.onDidDismiss(this.dismissHandler);
    toast.present();
    return;
  }
  addscore(i){
    this.rate = i
  }
  addscore1(i){
    this.qualityrate = i;
  }
  //获取任务详情
  getTaskDetail(){
    this.apiService.getTaskDetails({token:this.token,taskid:this.id},response => {
      if(response.json().status == 1){
        this.content = response.json().content.task;
        this.expectedtime = response.json().content.task.expectedtime;
        this.efficiency_score = response.json().content.task.efficiency;
        this.quality_score = response.json().content.task.quality;
        this.state = response.json().content.task.classname;
        let task_status = response.json().content.task.status;
        let task_createuser = response.json().content.task.createuser;
        let task_receiveuser = response.json().content.task.receiveuser;
        //初始化操作
        if(this.state == 'incomplete_state'){
          this.incomplete_vaule = 'finish'
        }else if(this.state == 'receive_state'){
          this.incomplete_vaule = 'catcher'
        }else if(this.state == 'check_state'){
          this.incomplete_vaule = 'completion'
        }

        if(task_createuser == this.username){
          this.check_status = true;
        }else{
          this.check_status = false;
        }
        if(task_receiveuser == this.username){
          this.receive_status = true;
        }else{
          this.receive_status = false;
        }
        if(task_createuser != this.username && task_receiveuser == this.username && task_status == 6){
          this.submit_status = true;
        }else{
          this.submit_status = false;
        }
        this.tasklog = response.json().content.tasklog;
      }
    })
  }
  //提醒任务
  remindTask(status,createuser,task_id){
    if(status == '待接手' && createuser == this.username){
      this.apiService.remindTasks({token: this.token,taskid: task_id},response => {
        if(response.json().status == 1){
          this.formTip(response.json().info);
        }else{
          this.formTip(response.json().info);
        }
      })
    }else{
      this.formTip('您没有修改权限');
    }
  }
  //完成任务
  taskOperation(){
    //完成任务
    if(this.incomplete_vaule == 'finish'){//完成任务
      this.body = {
        token: this.token,
        taskid: this.id,
        action: 6
      }
    }else if(this.incomplete_vaule == 'transfer'){//转手任务
      this.body = {
        token: this.token,
        taskid: this.id,
        action: 5,
        receiveuserid:this.form_content.recipient,
        note:this.form_content.instruction
      }
    }else if(this.incomplete_vaule == 'catcher'){//接受任务
      this.body = {
        token: this.token,
        taskid: this.id,
        action: 3,
        expectedtime:this.form_content.end_time
      }
    }else if(this.incomplete_vaule == 'returner'){//退回任务
      this.body = {
        token: this.token,
        taskid: this.id,
        action: 8,
        note: this.form_content.instruction
      }
    }else if(this.incomplete_vaule == 'completion'){//确认完成任务
      this.body = {
        token: this.token,
        taskid: this.id,
        action: 7,
        quality:(this.qualityrate+1)*20,
        efficiency:(this.rate+1)*20
      }
    }
    this.apiService.taskOperations(this.body,response => {
      if(response.json().status == 1){
        this.formTip(response.json().info);
        this.navCtrl.pop()
      }else{
        this.formTip(response.json().info);
      }
    })
  }
  //获取用户列表
  getUser(){
    this.apiService.getUsers({token:this.token},response => {
      if(response.json().status == 1){
        this.allcheckman = response.json().content;
      }
    })
  }
  //富文本编辑器相关
  saveSelection(){
    this.EditorService.saveSelection();
  }
  restoreSelection(){
    this.EditorService.restoreSelection();
  }

  formartDate (nowDate, status = true) {
    let y = nowDate.getFullYear();
    let m = nowDate.getMonth() + 1 < 10 ? "0" + (nowDate.getMonth() + 1) : nowDate.getMonth() + 1;
    let d = nowDate.getDate() < 10 ? "0" + nowDate.getDate() : nowDate.getDate();
    let h = nowDate.getHours();
    let i = nowDate.getMinutes();
    let s = nowDate.getSeconds();
    if (status) {
      return y + "-" + m + "-" + d
    }
    return y + "-" + m + "-" + d + " " + h + ":" + m + ':' + s
  }
}
